package com.luojun.service;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.luojun.dao.CommodityDao;
import com.luojun.entity.Commodity;
import com.luojun.util.HibernateUtil;

public class ACommodityService {
	Session session =null;
	Transaction tran = null;
	CommodityDao commoditydao = new CommodityDao();
	/**
	 * 删除商品
	 * @param commodityId
	 */
	public void del(int commodityId){
		session =HibernateUtil.getCurrentSession();
		tran = session.beginTransaction();
		commoditydao.del(commodityId);
		tran.commit();
	}
	/**
	 * 查看审核中商品
	 * @return
	 */
	public List<Commodity> find(){
		session =HibernateUtil.getCurrentSession();
		tran = session.beginTransaction();
		List<Commodity> cList = commoditydao.findbyStatus(2);
		tran.commit();
		return cList;
	}
	
	/**
	 * 根据商品id查找
	 * @param commodityId
	 * @return
	 */
	public Commodity findByCommodityId(int commodityId){
		session =HibernateUtil.getCurrentSession();
		tran = session.beginTransaction();
		List<Commodity> cList = commoditydao.findByCommodityId(commodityId);
		Commodity commodity = (Commodity)cList.get(0);
		tran.commit();
		return commodity;
	}
	/**
	 * 根据用户id查找
	 * @param userId
	 * @return
	 */
	public List<Commodity> findByUserId(int userId){
		session =HibernateUtil.getCurrentSession();
		tran = session.beginTransaction();
		List<Commodity> cList = commoditydao.findByUserId(userId);
		tran.commit();
		return cList;
	}
	/**
	 * 分页遍历
	 * @param pageNum
	 * @param pageSize
	 * status = 2
	 * @return
	 */
	public List<Commodity> findByPage(int nowpageNum,int pageSize){
		session =HibernateUtil.getCurrentSession();
		tran = session.beginTransaction();
		List<Commodity> cList = commoditydao.findByPage(nowpageNum, pageSize , 2);
		tran.commit();
		return cList;	
	}
	
	/**
	 * 获取总记录数
	 * status = 2
	 * @return
	 */
	public int findCountByPage(){
		session =HibernateUtil.getCurrentSession();
		tran = session.beginTransaction();
		int count =  commoditydao.findCount(2);
		tran.commit();
		return count;
	}
	/**
	 * 合格
	 * @param commodity
	 */
	public void passCommodity(Commodity commodity){
		session =HibernateUtil.getCurrentSession();
		tran = session.beginTransaction();
		commodity.setStatus(1);
		commoditydao.update(commodity);
		tran.commit();
	}
}
